package com.stefanie.redisdemo.init;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @author: stefanie-zy
 * @time: 2024/2/2-9:26
 * @description: 随服务自启动
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RedisInit implements CommandLineRunner {

    private final RedisTemplate redisTemplate;

    @Override
    public void run(String... args) throws Exception {
        User user = new User("stefanie", "123");
        redisTemplate.opsForValue().set("user:100", user);
        User user1 = (User) redisTemplate.opsForValue().get("user:100");
        log.info("user={}", user1);
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class User {
        private String userName;
        private String password;
    }
}
